//声音合成获取token
const appKey = 'SGcG47vTmTp98WNRDifaWAww'
const appSecret = 'vhtpLXGrMAYtRHxzoA5MqiL7Fhu2TlQE'
const apstr = 'https://openapi.baidu.com/oauth/2.0/token'
const vedio = 'https://tsn.baidu.com/text2audio'
function calVoice(callback) {
  wx.request({
    url: apstr, //仅为示例，并非真实的接口地址
    method: 'GET',
    data: {
      grant_type: 'client_credentials',
      client_id: appKey,
      client_secret: appSecret
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data) {
        var accsee_token = res.data.access_token;
        try {
          wx.setStorageSync('vtoken', accsee_token)
        } catch (e) {
        }
        callback(accsee_token);
      } else {
      }
    }
  })
}
//声音合成获取声音
function requestVoice(e,str){
  var tempstr = str
  // http://tsn.baidu.com/text2audio?tex=%e7%99%be%e5%ba%a6%e4%bd%a0%e5%a5%bd&lan=zh&cuid=***&ctp=1&tok=***
  // var ucodes = encodeURI(encodeURI(login_name));  
  var vedios = vedio + '?tex=' + tempstr + '&lan=zh&cuid=aaa&ctp=1&tok=' + e;
  var ucodeurl = encodeURI(vedios);
  console.log(ucodeurl);

  const backgroundAudioManager = wx.getBackgroundAudioManager()
  backgroundAudioManager.title = tempstr
  // backgroundAudioManager.epname = '此时此刻2'
  // backgroundAudioManager.singer = '许巍3'
  // backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'
  backgroundAudioManager.src = ucodeurl  // 设置了 src 之后会自动播
  backgroundAudioManager.onError((res) => {
    try {
      wx.setStorageSync('vtoken', '')
    } catch (e) {
    }
  })
  backgroundAudioManager.onCanplay((res) => {
  })
}
module.exports = {
  calVoice: calVoice,
  requestVoice: requestVoice,
}
